﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using Project.UI;
using XLua;

namespace Project
{
    [LuaCallCSharp]
    public class UIManager : MonoBehaviour
    {
        public static UIManager Instance;
        // Use this for initialization
        void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
            }
            
        }

        void Start()
        {
            //UIPackage.AddPackage("ui/Project");
            //XluaManager.Instance.luaenv.DoString("require 'Manager/UIManager'");
            //XluaManager.Instance.luaenv.Dispose();
            BindUI();
        }

        void BindUI()
        {

            //UIConfig.defaultFont = "DFPWaWaW5-GB";
            
            //UIObjectFactory.SetPackageItemExtension(UIPackage.GetItemURL(UIInitPageView.Package, UIInitPageView.ResName), typeof(UIInitPageView));
            //UIObjectFactory.SetPackageItemExtension(UIPackage.GetItemURL(UIMainPageView.Package, UIMainPageView.ResName), typeof(UIMainPageView));
            //UIObjectFactory.SetPackageItemExtension(UIPackage.GetItemURL(UIItemPageView.Package, UIItemPageView.ResName), typeof(UIItemPageView));

        }
        
        public ViewController AddInitPage()
        {
            GameManager.currentView = (UIInitPageView)UIPackage.CreateObject(UIInitPageView.Package, UIInitPageView.ResName);
            SetSize(GameManager.currentView);
            return GameManager.currentView;
        }

        public ViewController AddMainPage()
        {
            GameManager.currentView = (UIMainPageView)UIPackage.CreateObject(UIMainPageView.Package, UIMainPageView.ResName);
            SetSize(GameManager.currentView);
            return GameManager.currentView;
        }

        
        void SetSize(ViewController view)
        {
            view.SetSize(GRoot.inst.width, GRoot.inst.height);
            view.AddRelation(GRoot.inst, RelationType.Size);
        }


        public void RemoveView(ViewController viewController)
        {
            GRoot.inst.RemoveChild(viewController);

        }

        public void AddChildAtFrontView(ViewController viewController)
        {
            //GameManager.currentView.AddChild(view);
            GRoot.inst.AddChild(viewController);

        }
        
    }

}
